﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace LoadScript
{
    public partial class Frm_Config1 : Form
    {
        SQLScriptLoaderRelease loader;
        string oldServerName, oldDbName;
        string username, password;
        string conn;
        public Frm_Config1()
        {
            InitializeComponent();

            txtBatPath.Text  =ConfigurationManager.AppSettings["batchFilePath"];
            conn = ConfigurationManager.AppSettings["connectionString"];

            oldServerName=txtServerName.Text = SQLScriptLoaderRelease.GetServerName(conn);
            oldDbName=txtDbName.Text = SQLScriptLoaderRelease.GetDBName(conn);
            username = txtUsername.Text = SQLScriptLoaderRelease.GetLoginName(conn);
            password = txtPassword.Text = SQLScriptLoaderRelease.GetPassword(conn);
            //txtConnStr .Text =ConfigurationManager.AppSettings["connectionString"];
            txtScriptPath.Text = ConfigurationManager.AppSettings["scriptFilePath"];
        }

        private void btnSelectScriptPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath.Length > 0)
            {
                txtScriptPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSelectBatPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath.Length > 0)
            {
                txtBatPath.Text = folderBrowserDialog1.SelectedPath;
            }
            //ConfigurationManager.AppSettings["batchFilePath"] = folderBrowserDialog1.SelectedPath;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string qianzhui = "server=";
                
                string str = conn.Replace(qianzhui + oldServerName, qianzhui+txtServerName.Text.Trim());
                qianzhui = "initial catalog=";
                str = str.Replace(qianzhui + oldDbName, qianzhui+txtDbName.Text.Trim());

                qianzhui = "user id=";
                str = str.Replace(qianzhui + username, qianzhui + txtUsername.Text.Trim());
                qianzhui = "password=";
                str = str.Replace(qianzhui + password, qianzhui + txtPassword.Text.Trim());
                loader = new SQLScriptLoaderRelease(txtBatPath.Text.Trim(), str,txtScriptPath.Text.Trim());
                loader.CreateBatFile();
                loader.Load();
                loader.RunBatFile();
                MessageBox.Show("完成！");
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       
    }
}
